package com.itboye.hutouben.activity.mysetting;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

public class SendYzmThreeActivity extends BaseActivity implements Observer {
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题

    EditText send_three_num;
    ToggleButton send_three_tb;
    TextView send_three_ok;

    private String mobile;// 接上页面传来的手机号
    private String code;// 接上页面传来的验证码

    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_yzm_three);
        add_shap_title_tv.setText(R.string.zhaohui_pass);

        userPresenter=new UserPresenter(this);
        mobile = getIntent().getStringExtra("mobile");
        code = getIntent().getStringExtra("code");
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_icon:
                finish();
                break;

            case R.id.send_three_tb:// 密码可见
                if (send_three_tb.isChecked()) {
                    send_three_num
                            .setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    send_three_num.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                }
                break;
            case R.id.send_three_ok:// 完成
                if(TextUtils.isEmpty(send_three_num.getText().toString())){
                    ByAlert.alert("请输入密码");
                    return;
                }
                userPresenter.updatePass("+86",code,mobile,send_three_num.getText().toString());
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if(entity!=null){
            IsUtilUid.tianjia(entity);
            if(entity.getEventType()==UserPresenter.updata_pass_success){
                ByAlert.alert(entity.getData());
            }
            if(entity.getEventType()==UserPresenter.updata_pass_fail){
                ByAlert.alert(entity.getData());
            }
        }
    }
}
